<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.spuId ? $t('add') : $t('update')" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()"
      :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
      <el-form-item label="砂芯图号" prop="spuNo">
        <el-input v-model="dataForm.spuNo" :disabled="editable" placeholder="砂芯图号" maxlength="20"
          show-word-limit></el-input>
      </el-form-item>
      <el-form-item label="砂芯名称" prop="spuName">
        <el-input v-model="dataForm.spuName" placeholder="砂芯名称" maxlength="50" show-word-limit></el-input>
      </el-form-item>
      <el-form-item label="批次" prop="lotNo">
        <el-input v-model="dataForm.lotNo" placeholder="批次" maxlength="32" show-word-limit></el-input>
      </el-form-item>
      <el-form-item label="托盘类型" prop="contType">
        <el-select v-model="dataForm.contType" filterable multiple placeholder="请选择托盘类型"><el-option
            v-for="item in contTypeOption" :key="item.dictValue" :label="item.dictValue" :value="item.dictValue">
            <span style="float: left">{{ item.dictValue }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{
              item.dictLabel
            }}</span>
          </el-option></el-select>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{
        $t("confirm")
      }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from "lodash/debounce";
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        spuId: '',
        spuNo: '',
        spuName: '',
        lotNo: '',
        contType: '',
      },
      contTypeOption: [],
      dictType: "cont_type",
      editable: false,
    };
  },
  computed: {
    dataRule() {
      return {
        spuNo: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        spuName: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        lotNo: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        contType: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
      };
    },
  },
  methods: {
    init() {
      this.visible = true;
      this.editable = false;
      // 通用主键是id,所以需要将当前页面的spuId赋值
      this.dataForm.spuId = this.dataForm.id;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.spuId) {
          //如果是修改信息则编号不能更改
          this.editable = true;
          this.getInfo();
        }
      });
      this.getContTypeList();
    },
    // 1.获取信息
    getInfo() {
      this.$http
        .get(`/wms/spu-info/${this.dataForm.spuId}`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm.spuNo = res.data.spuNo;
          this.dataForm.spuName = res.data.spuName;
          this.dataForm.lotNo = res.data.lotNo;
          this.dataForm.contType = res.data.contType.split(",");
        })
        .catch(() => { });
    },
    //2.获取托盘类型信息
    getContTypeList() {
      this.$http
        .get(`/sys/dict/type/list`, {
          params: {
            dictType: this.dictType,
          },
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.contTypeOption = res.data;
        })
        .catch(() => { });
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
        this.$refs["dataForm"].validate((valid) => {
          if (!valid) {
            return false;
          }
          //将托盘类型重组
          this.dataForm.contType = this.dataForm.contType.join(",")

          this.$http[!this.dataForm.spuId ? "post" : "put"](
            "/wms/spu-info/",
            this.dataForm
          )
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            })
            .catch(() => { });
        });
      },
      1000,
      { leading: true, trailing: false }
    ),
  },
};
</script>
